package org.yookiee.support.config.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.yookiee.support.constants.DataCacheType;
import org.yookiee.support.core.EasyLocale;
import org.yookiee.support.core.I18NContext;
import org.yookiee.support.core.SysI18nAllocate;
import org.yookiee.support.core.datasource.I18nDatasource;
import org.yookiee.support.enums.Scope;
import org.yookiee.support.toolkits.DataTypeToolkit;
import org.yookiee.support.toolkits.I18nToolkit;
import org.yookiee.support.toolkits.JsonToolkit;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * FileName I18NAspect
 * Package org.jeecg.common.aspect
 * Description 处理国际化切面
 * author cpstory
 * create 2023-09-27 8:46
 * version V1.0
 */
@Aspect
@Component
@Slf4j
public class I18NAspect implements Ordered {

    private static final String LOG_MESSAGE = "easy-i18n processing data takes: {} ms";
    @Resource
    I18nDatasource i18nDatasource;

    @Pointcut("@annotation(org.yookiee.support.config.annotation.I18NTrans)")
    public void i18nPointCut() {
        // nothing to do
    }

    @Around("i18nPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        EasyLocale locale = I18NContext.get();
        Object resultData = point.proceed();
        if(resultData == null){
            return null;
        }
        // process simple data type
        if (DataTypeToolkit.isSimpleTypeWithoutString(resultData)) {
            return resultData;
        }

        Class aClass = I18nToolkit.getClass(resultData);
        Map<String,SysI18nAllocate> allocateMap = i18nDatasource.getScopeData(Scope.JAVA.getScope());

        if (allocateMap == null || allocateMap.isEmpty()){
            log.info("you have not config the language file,please check your language.properties file");
            return resultData;
        }

        if(DataTypeToolkit.isString(resultData)){
            String resultDatString = resultData.toString();
            List<String> params = I18nToolkit.findParams(resultDatString);
            String processed = I18nToolkit.dataProcess(locale.getLang(), resultDatString, allocateMap, params);
            long time = System.currentTimeMillis() - beginTime;
            log.info(LOG_MESSAGE,time);
            return processed;
        }

        String json = JsonToolkit.toJSON(resultData);
        List<String> params = I18nToolkit.findParams(json);
        if (null == params ){
            return resultData;
        }
        String processed = I18nToolkit.dataProcess(locale.getLang(), json, allocateMap, params);
        Object newResult = JsonToolkit.toBean(processed, aClass);
        long time = System.currentTimeMillis() - beginTime;
        log.info(LOG_MESSAGE,time);
        return newResult;
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }
}
